package com.microsoft.socialphoto.ui;

import java.util.ArrayList;
import java.util.List;

import org.odata4j.consumer.ODataConsumer;
import org.odata4j.core.OEntity;

import android.os.Bundle;
import android.widget.ListView;
import android.widget.TextView;

import com.microsoft.socialphoto.R;
import com.microsoft.socialphoto.SocialActivity;
import com.microsoft.socialphoto.adapter.CommentAdapter;
import com.microsoft.socialphoto.adapter.item.CommentItem;
import com.microsoft.socialphoto.odata.OData;

public class ShowComment extends SocialActivity {

	private long photoId;
	private ListView lvComment;
	private TextView tvNoComment;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		super.onCreate(savedInstanceState);

		Bundle extras = getIntent().getExtras();

		// get photo id
		if (null != extras)
			photoId = extras.getLong("photo_id");

		setContentView(R.layout.activity_show_comment);

		tvNoComment = (TextView) findViewById(R.id.no_comment);

		lvComment = (ListView) findViewById(R.id.comment_list);
		lvComment.setEmptyView(tvNoComment);
		lvComment.setAdapter(new CommentAdapter(this, R.id.btn_comment,
				getCommentData()));
	}

	private ArrayList<CommentItem> getCommentData() {
		ArrayList<CommentItem> list = new ArrayList<CommentItem>();

		ODataConsumer c = OData.getInstance();

		List<OEntity> comments = c
				.getEntities("Comments?$filter=Photo/Id%20eq%20" + photoId)
				.execute().toList();

		for (OEntity comment : comments) {
			CommentItem item = new CommentItem();

			item.setContent(comment.getProperty("Content", String.class)
					.getValue());
			list.add(item);
		}

		return list;
	}
}
